<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/goods' }">商品列表</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <br>
    <!-- 面包屑 -->
    <el-card>
      <!-- 添加商品信息 -->
      <el-alert
        title="消息提示的文案"
        type="info"
        center
        show-icon
      >
      </el-alert>
      <!-- 添加商品信息 -->
      <!-- 步骤条 -->
      <el-steps
        :space="200"
        :active="active*1"
        finish-status="success"
        style="margin-top:20px;margin-left:50px"
        align-center
      >
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!-- 步骤条 -->
      <el-form :model="formuser">
        <!-- 标签页 -->
        <el-tabs
          :tab-position="tabPosition"
          v-model="active"
          :before-leave="BeforeLeave"
          @tab-click="TabClick"
        >
          <el-tab-pane
            name="0"
            label="基本信息"
          >
            <!-- <el-form > -->
            <el-form-item label="商品名称">
              <el-input v-model="formuser.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格">
              <el-input
                type="number"
                v-model="formuser.goods_price"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品重量">
              <el-input
                type="number"
                v-model="formuser.goods_weight"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品数量">
              <el-input
                type="number"
                v-model="formuser.goods_number"
              ></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
              <br>
              <p>
                选择商品分类 ：
                {{JiLianModel}}
                <el-cascader
                  v-model="JiLianModel"
                  :options="JiLianData"
                  :props="Propsprops"
                  @change="XuanZe"
                ></el-cascader>
              </p>
            </el-form-item>
            <!-- </el-form> -->
          </el-tab-pane>
          <el-tab-pane
            name="1"
            label="商品参数"
          >
            <!-- 渲染动态参数部分 -->
            <el-form-item
              v-for="item in DongTai"
              :key="item.attr_id"
              :label="item.attr_name"
            >
              <el-checkbox-group v-model="item.attr_vals">
                <el-checkbox
                  v-for="(itm,ind) in item.attr_vals"
                  :key="ind"
                  :label="itm"
                ></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <!-- 渲染动态参数部分 -->
          </el-tab-pane>
          <el-tab-pane
            name="2"
            label="商品属性"
          >
            <!-- 静态资源渲染 -->
            <el-form-item
              v-for="item in JingTai"
              :key="item.attr_id"
              :label="item.attr_name"
            >
              <el-input v-model="item.attr_vals"></el-input>
            </el-form-item>
            <!-- 静态资源渲染 -->
          </el-tab-pane>
          <el-tab-pane
            name="3"
            label="商品图片"
          >
            <!-- 上传图片 -->
            <el-upload
              class="upload-demo"
              action="http://www.ysqorz.top:8888/api/private/v1/upload"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :on-success="handSuccess"
              :headers="headers"
              list-type="picture"
            >
              <el-button
                size="small"
                type="primary"
              >点击上传</el-button>
              <div
                slot="tip"
                class="el-upload__tip"
              >只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
            <!-- 上传图片 -->
          </el-tab-pane>
          <el-tab-pane
            name="4"
            label="商品内容"
          >

            <!-- 富文本编辑器 -->
            <quill-editor v-model="formuser.goods_introduce" />
            <!-- 富文本编辑器 -->
            <!-- 添加按钮 -->
            <el-button @click="AddGoodsList">添加商品</el-button>
            <!-- 添加按钮 -->
          </el-tab-pane>
        </el-tabs>
        <!-- 标签页 -->
      </el-form>

    </el-card>
  </div>
</template>

<script>
import _ from "lodash";
export default {
  data() {
    return {
      tabPosition: "left",
      active: "0",
      formuser: {
        goods_name: "",
        goods_cat: "",
        goods_price: 1,
        goods_number: 1,
        goods_weight: 1,
        goods_introduce: "",
        pics: [],
        attrs: []
      },
      //   选中的商品分类id
      JiLianModel: [],
      //   商品分类数据
      JiLianData: [],
      //   分类内容
      Propsprops: {
        expandTrigger: "hover",
        label: "cat_name",
        children: "children",
        value: "cat_id"
      },
      JingTai: [],
      DongTai: [],
      headers: {
        Authorization: sessionStorage.getItem("token")
      }
    };
  },
  created() {
    this.XuanZe();
  },
  methods: {
    // 请求发呢类数据
    async XuanZe(v) {
      console.log("XuanZe", v);
      let res = await this.$API.GoodsFenLei();
      console.log("级联菜单数据", res);
      this.JiLianData = res;
    },
    // 切换tab触发
    BeforeLeave(news, olds) {
      console.log("olds", olds);
      console.log("news", news);
      if (olds == 0 && this.JiLianModel.length < 3) {
        console.log(this.JiLianModel);
        this.$message({
          message: "您还没有选择商品分类",
          type: "warning"
        });
        return false;
      }
    },
    // 点击tabs触发
    // 请求渲染动态参数和静态参数
    async TabClick() {
      console.log("下表", this.active);
      if (this.active === "1") {
        let res = await this.$API.AddGoodsList(this.goodslist);
        console.log("添加商品动态数据", res);
        // 将字符串变成数组
        res.forEach(item => {
          item.attr_vals = item.attr_vals.split(",");
        });
        this.DongTai = res;
      } else if (this.active === "2") {
        let res = await this.$API.AddGoodsList(this.goodslist, "only");
        console.log("添加商品静态数据", res);
        this.JingTai = res;
      }
    },
    // 文件上传成功时的钩子
    handSuccess(response) {
      console.log("文件上传成功时的钩子", response);
      this.formuser.pics.push({ pic: response.data.tmp_path });
      // let obj = {
      //   pic: response.data.tmp_path
      // };
      // this.formuser.pics.push(obj);
      // console.log(this.formuser.pics, "11111");
    },
    // 点击已经上传的图片时。
    handlePreview() {},
    // 删除图片时
    handleRemove(file) {
      console.log("删除图片时", file);
      console.log("pics", this.formuser.pics);
      let index = this.formuser.pics.findIndex(item => {
        return item.pics === file.response.data.tmp_path;
      });
      console.log("下表", index);
      this.formuser.pics.splice(index, 1);
    },
    // 确认添加商品
    async AddGoodsList() {
      this.formuser.goods_cat = this.JiLianModel.join(",");
      console.log("转换成字符串后的", this.formuser.goods_cat);
      console.log("没转换成字符串", this.JiLianModel);
      //   动态深拷贝
      let DTarr = _.cloneDeep(this.DongTai);
      DTarr = DTarr.map(item => {
        return {
          attr_id: item.attr_id,
          attr_value: item.attr_vals.join(",")
        };
      });
      console.log("动态深拷贝", this.DongTai);
      //   静态深拷贝
      let JTarr = _.cloneDeep(this.JingTai);
      JTarr = JTarr.map(item => {
        return {
          attr_id: item.attr_id,
          attr_value: item.attr_vals
        };
      });
      //   合并并赋值
      this.formuser.attrs = [...DTarr, ...JTarr];
      console.log("赋值后的数据", this.formuser);
      let HeBingarr = _.cloneDeep(this.formuser);
      console.log("总数据", this.formuser);
      //   请求接口
      let res = await this.$API.AddGoods(HeBingarr);
      console.log("是否添加成功", res);
      this.$router.push("/goods");
    }
  },
  computed: {
    // 取数组中最后一个id
    goodslist() {
      return this.JiLianModel[this.JiLianModel.length - 1];
    }
  }
};
</script>

<style scoped >
::v-deep .ql-editor {
  height: 300px;
}
</style>
